const app = getApp()

Page({
  data: {
    address_id: 0,//修改or新建
    name: '',
    phone: '',
    address: '',
    code: '',
    is_show: true,
    provinces: [],
    citys: [],
    areas: [],
    value: [0, 0, 0],
    all_area: [],
    area_text: '请选择所在区',
    addressMenuIsShow: false,
    animationAddressMenu: {},
    areaInfo: ''
  },
  onLoad: function (options) {
    //判断新建还是修改
    if (options.address_id != undefined) {
      //获取地址
      this.setData({
        address_id: options.address_id,
      });

      this.getAddress();
    } else {
      //获取地址
      this.setData({
        address_id: '',
      });
    }
  },

  //获取地区
  getArea: function () {
    var _this = this;
    tt.request({
      url: app.globalData.url + 'index.php?act=member&op=area_list&key=' + encodeURI(app.globalData.token) + '&area_id=0',
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        var id = res.data.datas.province_list[0].area_id;
        _this.setData({
          provinces: res.data.datas.province_list,
          citys: res.data.datas.city_list[id],
          areas: res.data.datas.area_list[res.data.datas.city_list[id][0].area_id],
          all_area: res.data.datas
        })
      },
      fail(res) {
        console.log(`request调用失败`);
      }
    })
  },
  //地区选择
  cityChange: function (e) {
    var value = e.detail.value
    var provinces = this.data.provinces
    var citys = this.data.citys
    var areas = this.data.areas
    var provinceNum = value[0]
    var cityNum = value[1]
    var countyNum = value[2]
    var all_area = this.data.all_area
    // 如果省份选择项和之前不一样，表示滑动了省份，此时市默认是省的第一组数据，
    if (provinces.length < 1) {
      return false;
    }
    if (this.data.value[0] != provinceNum) {
      var id = provinces[provinceNum].area_id;

      this.setData({
        value: [provinceNum, 0, 0],
        citys: all_area.city_list[id],
        areas: all_area.area_list[all_area.city_list[id][0].area_id],
      })
    } else if (this.data.value[1] != cityNum) {
      // 滑动选择了第二项数据，即市，此时区显示省市对应的第一组数据
      var id = citys[cityNum].area_id
      this.setData({
        value: [provinceNum, cityNum, 0],
        areas: all_area.area_list[citys[cityNum].area_id],
      })
    } else {
      // 滑动选择了区
      this.setData({
        value: [provinceNum, cityNum, countyNum]
      })
    }
  },
  //显示地区选择按钮
  showCheck: function () {
    this.setData({
      addressMenuIsShow: true
    })
  },
  // 点击地区选择取消按钮
  cityCancel: function (e) {
    this.setData({
      addressMenuIsShow: false
    })
  },
  // 点击地区选择确定按钮
  citySure: function (e) {
    var that = this
    var city = that.data.city
    var value = that.data.value
    //that.startAddressAnimation(false)
    that.cityCancel();
    // 将选择的城市信息显示到输入框
    if (that.data.areas[value[2]]) {
      var areaInfo = that.data.provinces[value[0]].area_name + ' ' + that.data.citys[value[1]].area_name + ' ' + that.data.areas[value[2]].area_name
    } else {
      var areaInfo = that.data.provinces[value[0]].area_name + ' ' + that.data.citys[value[1]].area_name
    }

    that.setData({
      areaInfo: areaInfo,
      area_text: areaInfo
    })
  },
  /**
  * 生命周期函数--监听页面显示
  */
  onShow: function () {
    this.getArea();
  },

  /**
   * 修改名字
   */
  confirmName: function (e) {
    this.setData({
      name: e.detail.value,
    })
  },
  /**
   * 输入地址
   */
  confirmAddress: function (e) {
    this.setData({
      address: e.detail.value,
    })
  },
  /**
   * 输入手机号
   */
  confirmPhone: function (e) {
    this.setData({
      phone: e.detail.value,
    })
  },

  /**
     * 输入邮编
     */
  confirmCode: function (e) {
    this.setData({
      code: e.detail.value,
    })
  },

  saveAddress: function () {
    var _this = this;
    var phone = _this.data.phone;
    if(!_this.isPoneAvailable(phone)){
      tt.showToast({
        title:'请输入正确手机号',
        icon:'none'
      })
    }
    var name = _this.data.name;
    var address = _this.data.address;
    //var region = _this.data.region.join(' ');
    var region = _this.data.areaInfo;
    var data = { act: 'toutiao_member', op: 'saveAddress', true_name: name, mob_phone: phone, area_info: region, address: address };

    if (_this.data.address_id != '') {
      data.address_id = _this.data.address_id
    }

    _this.setData({
      is_show: false
    });

    wx: wx.request({
      url: app.globalData.url + 'index.php?key=' + encodeURI(app.globalData.token),
      data: data,
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        if (res.data.code == 200) {
          wx: wx.showToast({
            title: res.data.datas,
            mask: true,
          });

          setTimeout(function () {
            tt.navigateBack({})
          }, 500);
        } else {
          wx: wx.showToast({
            title: res.data.datas.error,
            mask: true,
            icon: 'none',
          });
          _this.setData({
            is_show: true
          });
        }
      },
    })
  },

  /**
   * 判断手机号
   */
  isPoneAvailable: function (str) {
    var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (!myreg.test(str)) {
      return false;
    } else {
      return true;
    }
  },


  /**
   * 获取地址
   */
  getAddress: function () {
    var _this = this;
    tt.showLoading({
      title:'加载中...'
    });
    var region = '';
    wx.request({
      url: app.globalData.url + 'index.php?act=member&op=address_info&address_id=' + _this.data.address_id + '&key=' + encodeURI(app.globalData.token),
      success: function (res) {
        tt.hideLoading();
        if (res.data.code == 200) {
          //region = res.data.datas.address_info.area_info.split(' ');

          _this.setData({
            name: res.data.datas.address_info.true_name,
            phone: res.data.datas.address_info.mob_phone,
            address: res.data.datas.address_info.address,
            //region: region,
            areaInfo: res.data.datas.address_info.area_info,
            area_text: res.data.datas.address_info.area_info
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.data.datas.error,
          });
          setTimeout(function () {

          }, 3000);
        }
      }
    })
  }
})
